﻿namespace NServiceBus.AcceptanceTests.Core.Diagnostics;

using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using AcceptanceTesting;
using AcceptanceTesting.Customization;
using EndpointTemplates;
using Logging;
using NUnit.Framework;

public class When_license_expired : NServiceBusAcceptanceTest
{
    [Test]
    public async Task Should_add_the_license_diagnostic_headers_to_audited_messages()
    {
        var context = await Scenario.Define<Context>()
            .WithEndpoint<EndpointWithAuditOn>(b => b.When(session => session.SendLocal(new MessageToBeAudited())))
            .WithEndpoint<AuditSpyEndpoint>()
            .Done(c => c.Done)
            .Run();

        Assert.That(context.HasDiagnosticLicensingHeaders, Is.True);

        if (Debugger.IsAttached)
        {
            Assert.That(context.Logs.Any(m => m.Level == LogLevel.Error && m.Message.StartsWith("Your license has expired")), Is.True, "Error should be logged");
        }
    }

    static string ExpiredLicense = @"<?xml version=""1.0"" encoding=""utf-8""?>
<license id = ""b13ba7a3-5fe8-4745-a041-2d6a9f7462cf"" expiration=""2015-03-18T00:00:00.0000000"" type=""Subscription"" Applications=""All"" NumberOfNodes=""4"" UpgradeProtectionExpiration=""2015-03-18"">
  <name>Ultimate Test</name>
  <Signature xmlns = ""http://www.w3.org/2000/09/xmldsig#"">
    <SignedInfo>
      <CanonicalizationMethod Algorithm= ""http://www.w3.org/TR/2001/REC-xml-c14n-20010315"" />
      <SignatureMethod Algorithm= ""http://www.w3.org/2000/09/xmldsig#rsa-sha1"" />
      <Reference URI= """">
        <Transforms>
          <Transform Algorithm= ""http://www.w3.org/2000/09/xmldsig#enveloped-signature"" />
        </Transforms>
        <DigestMethod Algorithm= ""http://www.w3.org/2000/09/xmldsig#sha1"" />
        <DigestValue>kz07xp2x3tjk+ixQglCHq40RJg8=</DigestValue>
      </Reference>
    </SignedInfo>
    <SignatureValue>WN0zCL3i2vvwtPFI7/Qbo8ymhJFeYpauFqFbuFynOfWrKd5PMfcY1ToWZyz1vs6dLFL9kPngtVRX9yZZXC1y6la8oS/rnBq0Jwm2pFqCtIVtXKee93dTTx7Bij9x7XUBtAVpZDszbZPfLnrdHwS4BFn4CTvOJRiSUEB1ks1ONiQ=</SignatureValue>
  </Signature>
</license>
";

    public class Context : ScenarioContext
    {
        public bool Done { get; set; }
        public bool HasDiagnosticLicensingHeaders { get; set; }
    }

    public class EndpointWithAuditOn : EndpointConfigurationBuilder
    {
        public EndpointWithAuditOn()
        {
            EndpointSetup<DefaultServer>(c =>
            {
                c.License(ExpiredLicense);
                c.AuditProcessedMessagesTo<AuditSpyEndpoint>();
            });
        }

        public class MessageToBeAuditedHandler : IHandleMessages<MessageToBeAudited>
        {
            public Task Handle(MessageToBeAudited message, IMessageHandlerContext context)
            {
                return Task.CompletedTask;
            }
        }
    }

    class AuditSpyEndpoint : EndpointConfigurationBuilder
    {
        public AuditSpyEndpoint()
        {
            EndpointSetup<DefaultServer>();
        }

        public class MessageToBeAuditedHandler : IHandleMessages<MessageToBeAudited>
        {
            public MessageToBeAuditedHandler(Context context)
            {
                testContext = context;
            }

            public Task Handle(MessageToBeAudited message, IMessageHandlerContext context)
            {
                testContext.HasDiagnosticLicensingHeaders = context.MessageHeaders.TryGetValue(Headers.HasLicenseExpired, out _);

                testContext.Done = true;

                return Task.CompletedTask;
            }

            Context testContext;
        }
    }

    public class MessageToBeAudited : IMessage
    {
    }
}